<template>
  <div class="login-group">
    <div class="login-form">
      <h2>欢迎登录无人超市后台管理系统</h2>
      <el-form ref="loginForm" :model="loginInfo" :rules="rules" label-position="right" label-width="80px" @keydown.enter.native="login">
        <el-form-item prop="username" label="用户名">
          <el-input v-model="loginInfo.username" prefix-icon="el-icon-user" placeholder="用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password" label="密码">
          <el-input v-model="loginInfo.password" prefix-icon="el-icon-lock" placeholder="密码" show-password></el-input>
        </el-form-item>
        <el-form-item prop="captcha" label="验证码">
          <el-input v-model="loginInfo.captcha" prefix-icon="el-icon-data-line" placeholder="验证码">
            <template slot="append">
              <img :src="captchaSrc" @click="getCaptcha" style="display:block;height: 40px;width: 120px" alt="">
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="common" @click="restForm" style="margin-left: 20px">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { uuid } from 'vue-uuid'
export default {
  name: 'Login',
  data () {
    return {
      loginInfo: {
        username: '',
        password: '',
        uuid: '',
        captcha: ''
      },
      captchaSrc: '',
      rules: {
        username: [
          {
            required: true,
            message: '用户名不能为空',
            trigger: 'blur'
          }, {
            min: 4,
            max: 16,
            message: '用户名长度在4-16之间',
            trigger: 'blur'
          }
        ],
        password: [{
          required: true,
          message: '密码不能为空',
          trigger: 'blur'
        }],
        captcha: [
          {
            required: true,
            message: '验证码不能为空',
            trigger: 'blur'
          }, {
            min: 1,
            max: 5,
            message: '验证码长度为5',
            trigger: 'blur'
          }
        ]
      },
      loading: ''
    }
  },
  methods: {
    getCaptcha () {
      this.loginInfo.uuid = uuid.v4()
      this.captchaSrc = `${process.env.VUE_APP_BASE_API}/captcha.jpg?uuid=${this.loginInfo.uuid}`
    },
    login () {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          /* 开启遮罩层，在ajax请求完成后关闭 */
          this.loading = this.$loading({ text: '加载中...' })
          this.$http.post('/sys/login', this.loginInfo)
            .then(data => {
              if (data.code === 0) {
                this.$message({
                  message: '登录成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.$cookies.set('token', data.token)
                    this.$router.replace('/')
                  }
                })
              } else {
                this.$alert(data.msg)
                this.getCaptcha()
                this.loginInfo.password = ''
                this.loginInfo.captcha = ''
              }
            }).catch(e => {
              console.log(e)
            }).finally(() => {
              this.loading.close()
            })
        }
      })
    },
    restForm () {
      this.$refs.loginForm.resetFields()
    }
  },
  created () {
    this.getCaptcha()
  }
}
</script>

<style scoped lang="scss">
h2{
  margin-left: 110px;
  margin-bottom: 10px;
}
.login-group{
  height: 100vh;
  width: 100vw;
  overflow: hidden;
  background: url("../assets/images/login-bg.jpg") no-repeat center/cover;
  .login-form{
    height: 40vh;
    width: 35vw;
    background-color: rgba(255,255,255,.5);
    border-radius: 17px;
    margin: 30vh auto;
    padding: 10px 30px;
  }
}
</style>
<style>
.el-input-group__append{
  padding: 0;
  border: none;
  height: 40px;
}
.el-form-item__label{
  color: #545c64;
  font-weight: 800;
}
</style>
